package com.hex.liyue.usermodule.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hex.liyue.R;
import com.hex.liyue.commonmodule.activity.BaseActivity;
import com.hex.liyue.commonmodule.utils.L;
import com.hex.liyue.commonmodule.view.recycleview.YRecycleview;
import com.hex.liyue.usermodule.adapter.AdapterWallet;
import com.hex.liyue.usermodule.bean.MoreRecordsResultBean;
import com.hex.liyue.usermodule.bean.MyWalletResultbean;
import com.hex.liyue.usermodule.presenter.IMoreRecordsView;
import com.hex.liyue.usermodule.presenter.MoreRecordsPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by WJ on 2016/6/26.
 */
public class MoreRecordsActivity extends BaseActivity implements IMoreRecordsView, YRecycleview.RefreshAndLoadMoreListener {
    @Bind(R.id.iv_toobar_back)
    ImageView ivToobarBack;
    @Bind(R.id.tv_toobar_title)
    TextView tvToobarTitle;
    @Bind(R.id.rv_morefriends)
    YRecycleview rvMorefriends;

    private MoreRecordsPresenter moreRecordsPresenter;
    private int page = 1, page_num = 10;
    private List<MyWalletResultbean.DataBean.RecordsBean> list = new ArrayList<>();

    @Override
    protected void bindPresenter() {
        moreRecordsPresenter = new MoreRecordsPresenter(this);
    }

    @Override
    protected void initData(Bundle extras) {
        moreRecordsPresenter.getTransactionRecords(page, page_num);

    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_morefriends;
    }

    @Override
    protected void initToolbar() {
        ivToobarBack.setVisibility(View.VISIBLE);
        tvToobarTitle.setText("交易记录");
    }


    @OnClick({R.id.iv_toobar_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_toobar_back:
                onBackPressed();
                break;
        }
    }

    @Override
    public void showLod(String msg) {
        showLoad(this, msg);
    }

    @Override
    public void closeLod() {
        closeLoad();
    }

    @Override
    public void showT(String msg) {
        showToast(this, msg);
    }

    private AdapterWallet adapterWallet;
    @Override
    public void setAdapter(MoreRecordsResultBean moreRecordsResultBean) {
        if (page == 1) {
            list.clear();
        }
        if (moreRecordsResultBean != null) {
            if (moreRecordsResultBean.data.size() == 0 && page > 1) {
                page--;
            }
            for (MyWalletResultbean.DataBean.RecordsBean dataBean : moreRecordsResultBean.data) {
                list.add(dataBean);
            }
        }
        if(adapterWallet==null){
            adapterWallet=new AdapterWallet();
            rvMorefriends.setLayoutManager(new LinearLayoutManager(this));
            rvMorefriends.setRefreshAndLoadMoreListener(this);
            rvMorefriends.setAdapter(adapterWallet);
        }
        adapterWallet.setRecords(list);
        adapterWallet.notifyDataSetChanged();
        rvMorefriends.reSetStatus();
        L.d("WJ", page + "=size=" + moreRecordsResultBean.data.size());
    }

    @Override
    public void onRefresh() {
        page = 1;
        moreRecordsPresenter.getTransactionRecords(page, page_num);
    }

    @Override
    public void onLoadMore() {
        page++;
        moreRecordsPresenter.getTransactionRecords(page, page_num);
    }
}
